﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using ApplLogic.ApplDbMediators;
using ApplLogic.Exceptions;
using Utilities.GlobalData;
using Utilities.XmlReader;

namespace ApplLogic.LogicalServices
{
    public class EmailHandler
    {
        private EmailSender m_EmailSender = null;

        public EmailHandler()
        {
            m_EmailSender = new EmailSender(MyXmlReader.GetValueOfGeneralSetting(eGeneralSettingName.EmailAdress),
                MyXmlReader.GetValueOfGeneralSetting(eGeneralSettingName.EmailPassword));
        }

        public void NotifyAllWorkersForNewShiftCreation(DateTime startTime, DateTime endTime, string additionalInfo)
        {
            string aditedAdditionalInfostring = additionalInfo != ""
                                 ? EmailMessages.GeneralAdittionalManagerInfo + additionalInfo
                                 : additionalInfo;
            foreach (var applWorker in ApplDbWorkerMediator.Instance.GetAllWorkers())
            {
                if (applWorker.PersonalDetails.Id == "305837726")
                m_EmailSender.SendSingleMail(applWorker.PersonalDetails.EmailAdress 
                    , string.Format("{0}{1},\n\n{2}{3}\n{4}", EmailMessages.GeneralMailOpening,applWorker.PersonalDetails.FullName
                    , EmailMessages.NewWorkingOrderBody, aditedAdditionalInfostring, EmailMessages.GeneralEnding)                     
                   ,string.Format("{0} for dates: {1} - {2}. {3}",
                    EmailMessages.NewWorkingOrderTitle, startTime.ToShortDateString(), endTime.ToShortDateString(),
                    EmailMessages.GeneralDoNotReply));
            }
        }

        public void NotifyWorkersThatnNotSendRequests(DateTime startTime, DateTime endTime, string additionalInfo)
        {
            string aditedAdditionalInfostring = additionalInfo != ""
                                             ? EmailMessages.GeneralAdittionalManagerInfo + additionalInfo
                                             : additionalInfo;
            foreach (var applWorker in ApplDbRequestMediator.Instance.GetAllWorkersThatNotAskedForanyShiftInRange(startTime, endTime))
            {
                if(applWorker.PersonalDetails.Id == "305837726")
                m_EmailSender.SendSingleMail(applWorker.PersonalDetails.EmailAdress,
                                             string.Format("{0}{1},\n\n{2}{3}\n{4}",
                                                           EmailMessages.GeneralMailOpening,
                                                           applWorker.PersonalDetails.FullName,
                                                           EmailMessages.MissingShiftRequestBody,
                                                           aditedAdditionalInfostring, EmailMessages.GeneralEnding),
                                             string.Format("{0} for dates: {1} - {2}. {3}",
                                                           EmailMessages.MissingShiftRequestTitle,
                                                           startTime.ToShortDateString(), endTime.ToShortDateString(),
                                                           EmailMessages.GeneralDoNotReply));
            }
        }

        private static class EmailMessages
        {
            public const string GeneralMailOpening = "Hello ";
            public const string GeneralDoNotReply = "(Do Not Reply)";
            public const string GeneralEnding = "\nRegards,\nShiftCreator Admin";
            public const string GeneralAdittionalManagerInfo = "\nAdditional Manager Info:\n";
            public const string NewWorkingOrderTitle = "New working order ";

            public const string NewWorkingOrderBody = "New working order is ready.\nPlease login to the application to see" +
                                                         " your shifts for the next week.\n";
            public const string MissingShiftRequestTitle = "Missing shift request ";
            public const string MissingShiftRequestBody = "You still haven't sent a shift request for the next week.\n"
                                                           + "Please send you request as soon as possible\n";
        }

        private class EmailSender
        {
            private string m_SenderAdress = string.Empty;
            private string m_SenderPassword = string.Empty;
            

            public EmailSender(string senderAddress, string senderPassword)
            {
                setAllDefaults(senderAddress, senderPassword);
            }

            private void setAllDefaults(string senderAddress, string senderPassword)
            {
                m_SenderAdress = senderAddress;
                m_SenderPassword = senderPassword;
            }

            public void SendSingleMail(string recipientMailAdress, string body, string title)
            {
                try
                {
                    SmtpClient smtpClient = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Credentials = new NetworkCredential(m_SenderAdress, m_SenderPassword),
                        Timeout = 10000
                    };
                    MailMessage message = new MailMessage(m_SenderAdress, recipientMailAdress, title, body);
                    smtpClient.Send(message);
                }
                catch (Exception exx)
                {
                    //throw new LogicalException(string.Format("Failed to send mail to {0} with exception: {1}",
                                                             //recipientMailAdress, exx.Message));
                }
            }
        }
    }

}
